package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.User;
import ua.kharkov.knure.mikhaylov.st4.db.enums.Role;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Redirection command - browse payment creation page
 * 
 * @author Saint
 * 
 */
public class BrowseCreatePaymentPageCommand extends Command {

	// WARNING: THIS CODE MAY BE HARMFUL

	private static final long serialVersionUID = -4074573367270509876L;
	private static final Logger log = Logger
			.getLogger(BrowseCreatePaymentPageCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		log.debug("Command starts");

		HttpSession session = request.getSession();

		session.setAttribute(
				"users",
				DBManager.getInstance().findUsersByRoleExceptOne(Role.CLIENT,
						(User) session.getAttribute("user")));

		log.debug("Users extracted");

		// unsetting attributes. They are used in bad code
		session.setAttribute("targetUser", null);
		session.setAttribute("senderAccounts", null);
		session.setAttribute("senderAccount", null);
		session.setAttribute("recieverAccounts", null);
		session.setAttribute("recieverAccount", null);

		log.debug("Command finished");
		return Path.PAGE_CREATE_PAYMENT;
	}

}
